Page({
    data: {
         menuList:[],
         goodsList:[],
         selectId:null,
         num:0,
         current:1,
         pagesize:20,
         toTop:false,
         moreText:'加载更多...',
         totalPage:1,
         showA:false
         
    },
    turnInvitation(){
      this.setData({
        showA:false
      })
     wx.navigateTo({
            url: '/pages/invitation/invitation'
        })
    },
    closeInvitation(){
      console.log('关闭')
      this.setData({
        showA:false
      })
    },
    getFloatView(){
      var _self=this;
         wx.request({
            url: getApp().globalData.baseUrl +'/yx/userScreen/isScreen?openid='+this.data.userInfo.openid,
            method:"GET",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
             console.log('res.data.obj*******')
             console.log(res.data.obj)
             _self.setData({
               showA:(res.data.obj==0?false:true)
             })
             
            
         }
       })
    },
    onLoad:function(opt){
      //  var userInfo= wx.getStorageSync('userInfo');
      //   this.setData({
      //       userInfo:userInfo
      //   })
      
      

    },
    onShow:function(){
       var searchMode= wx.getStorageSync('searchMode');
       // console.log()
       console.log('searchMode=='+searchMode)
       if (searchMode) {
        wx.setStorageSync('searchMode',null);
        this.getGoods2(searchMode)
       }
       // if (serc) {}
        getApp().getUserInfo().then(res=>{
       
         this.setData({
          userInfo:res,
         })  
         if (this.data.menuList.length>1) {

         }else{
          this.getMents();
         }
         
         this.getNum();
         this.getFloatView();
         
         // this.getAllSheBeiForTuiJian(address.province);
       })
    },
    getMents:function(){
    var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/menusType',
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                    menuList:res.data.obj,
                    selectId:res.data.obj[0].id
                })
                _self.getGoods()
              }
            console.log(res)  
            
         }
       })
    },
    getNum:function(){
       var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/shopping/find/num?openid='+this.data.userInfo.openid,
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                  num:res.data.obj
                })
              }
            console.log(res)  
            
         }
       })


    },
    chooseType:function(e){
      console.log(e.currentTarget.dataset.id)
      this.setData({
        selectId:e.currentTarget.dataset.id,
         current:1,
         pagesize:20,
         goodsList:[]

      })
      this.getGoods()
    },
     onPullDownRefresh:function () {
      console.log('刷新')
      this.setData({
        goodsList:[],
        current:1,
        pagesize:20
      })
      this.getGoods();
      var  timer=setTimeout(function(){
        clearTimeout(timer);
          wx.stopPullDownRefresh();
      },2000)
    },
    //上拉加载更多
     onReachBottom:function(){
        console.log('加载更多')
        this.setData({
          current:this.data.current+1
        })
        if (this.data.current<=this.data.totalPage) {
          this.getGoods();
        }else{
          this.setData({
            moreText:'没有更多了'
          })
        }
    },
    getGoods:function(){
       var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/parts/getList?menuTypeId='+this.data.selectId+'&current='+this.data.current+'&pagesize='+this.data.pagesize,
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                    goodsList:_self.data.goodsList.concat(res.data.obj.list),
                    totalPage:res.data.obj.pages
                })
              }
            console.log(res)  
            
         }
       })  
    },
    getGoods2:function(searchMode){
       var _self=this;
        wx.request({
            url:getApp().globalData.baseUrl+'/pj/parts/getList?name='+searchMode,
            method:"get",
            header:{
               'openid':this.data.userInfo.openid, //公用请求头
               'content-type': 'application/json'
            },
            success: function(res) {
              if (res.data.code==200) {
                _self.setData({
                    goodsList:res.data.obj.list
                })
              }
            console.log(res)  
            
         }
       })  
    },
    turnDetail:function(e){
        console.log(e);
        wx.navigateTo({
        url: '/pages/goodsDetail/goodsDetail?id='+e.currentTarget.dataset.code
      })
    },
    turnPage:function(){
      var userInfo= wx.getStorageSync('userInfo');
      if (userInfo.phone==null||userInfo.phone.length!=11) {
         this.setData({
         showToast:true
        })
      }else{
         wx.navigateTo({
          url: '/pages/shopMarket/shopMarket'
         })
      }
    
    },


    turnPage1:function(){
     
         wx.navigateTo({
           url: '/pages/invitation/invitation'
         })
      
    
    },

    toSearch:function(){
       wx.navigateTo({
        url: '/pages/search/search'
      })
    },

    onCannel:function () {
     this.setData({
       showToast:false
     })
    },
    onLoginSuccess:function () {
       this.setData({
       showToast:false
      })
    },
    onShareAppMessage:function(){
         return {
              title: '塔塔配件商城',
              imageUrl: '' // 图片 URL
          }
    },
    onShareTimeline:function(){
         return {
              title: '塔塔配件商城',
              imageUrl: '' // 图片 URL
          }
    },
   

});





